package com.itheima;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        //"https://movie.douban.com/top250?start=0&filter="
        ArrayList<Movie> list01 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String url = "https://movie.douban.com/top250?start=" + (i * 25) + "&filter=";
            getMovies(list01, url);
        }
        for (int i = 0; i < list01.size(); i++) {
            System.out.println(list01.get(i));
        }
    }

    public static void getMovies(ArrayList<Movie> list01, String url) throws IOException {
        //document 获取到文档对象
        Document document = Jsoup.connect(url)
                .maxBodySize(0)
                .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")
                .timeout(6000)
                .get();

        Element eList = document.getElementsByClass("grid_view").get(0);
        //获取所有的电影的标签 得到一个集合
        Elements movieList = eList.getElementsByClass("item");
        for (int i = 0; i < movieList.size(); i++) {
            //获取到每个电影的标签
            Element movie = movieList.get(i);
            Element pic = movie.getElementsByClass("pic").get(0);
            //获取img标签的src属性值
            String img_url = pic.getElementsByTag("img").attr("src");
            //System.out.println(img_url);
            //电影信息
            Element info = movie.getElementsByClass("info").get(0);
            //获取电影名字  text是获取标签中间的文本
            String title = info.getElementsByClass("title").get(0).text();
            String ratingNum = info.getElementsByClass("rating_num").get(0).text();
            String command_Count = info.getElementsByClass("star").get(0).child(3).text();
            int commandCount = Integer.parseInt(command_Count.replace("人评价", ""));
//            System.out.println(title+"_"+ratingNum+"_"+commandCount);
            list01.add(new Movie(img_url, title, ratingNum, commandCount));
        }
    }
}




